let setCookieBCGeolocation = function (name, value, remove = false) { value = encodeURIComponent(value); let expires = ""; let date = new Date(); date.setTime(date.getTime() + 6 * 60 * 60 * 1000); expires = "; expires=" + (remove ? 'Thu, 01 Jan 1970 00:00:00 UTC' : date.toUTCString()); const isHttps = location.protocol === 'https:'; document.cookie = name + "=" + (value || "") + expires + "; path=/" + (isHttps ? "; Secure; SameSite=None" : ""); if (remove) { alert('Geolocation cookie data cleared'); } }; window.addEventListener('DOMContentLoaded', function() { const cookieName = 'bc-geolocation-data'; const cookieValue = getCookieValue(cookieName); const clearCookieBtn = document.querySelector('#wp-admin-bar-bc-geolocation-toolbar__cookie-clear > a'); if (clearCookieBtn) { clearCookieBtn.style.color = '#ff4500'; clearCookieBtn.addEventListener('click', function() { setCookieBCGeolocation(cookieName, cookieValue, true); }); } if (!cookieValue || cookieValue === '----') { const request = new XMLHttpRequest(); request.open("GET", ajaxObject.ipApiUrl, true); request.onload = function () { const cookieInfo = JSON.parse(request.response); const countryCode = cookieInfo.countryCode ? cookieInfo.countryCode : ''; const country = cookieInfo.country ? cookieInfo.country : ''; const region = cookieInfo.region ? cookieInfo.region : ''; const regionName = cookieInfo.regionName ? cookieInfo.regionName.replaceAll('-','_') : ''; const city = cookieInfo.city ? cookieInfo.city : ''; const cookieValue = `${countryCode}-${country}-${region}-${regionName}-${city}`; setCookieBCGeolocation(cookieName, cookieValue); }; request.onerror = function () { if (debug) { console.log("Config file does not exist") } }; request.send(); } }); const getCookieValue = (cookieName) => { let cookie = {}; document.cookie.split(';').forEach(function(element) { let [key, value] = element.split('='); cookie[key.trim()] = value; }); return cookie[cookieName]; }